package 区间相关问题;

import java.util.*;

public class 区间合并 {

    public int[][] merge(int[][] intervals) {
        Arrays.sort(intervals, new Comparator<int[]>() {
            @Override
            public int compare(int[] ints, int[] t1) {
                return ints[0]-t1[0];
            }
        });
        List<int[]> list = new ArrayList();
        list.add(intervals[0]);
        for (int i = 1; i < intervals.length; i++) {
            int[] last = list.get(list.size() - 1);
            int[] curr = intervals[i];
            if (curr[0]<=last[1])
                last[1]= Math.max(last[1],curr[1]);
            else
                list.add(curr);
        }
        int[][] res = new int[list.size()][2];
        for (int i = 0; i < list.size(); i++) {
            res[i]=list.get(i);
        }
        return res;
    }
}
